<?php
namespace app\admin\controller;

class Report extends Common
{
    public function newreport_list()
    { 
        $ret = db('newreport')->alias('r')->join('user u','r.user_id=u.user_id')->join('new n','r.new_id=n.new_id')
        ->field('r.*,u.user_name,n.title')->order("addtime desc")->paginate(2);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',9);
        return view('newreport_list');
    }

    public function notereport_list()
    {   
        $ret = db('notereport')->alias('r')->join('user u','r.user_id=u.user_id')
        ->join('note n','r.note_id=n.note_id')->field('r.*,u.user_name,n.title')->order("addtime desc")->paginate(2);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',10);
        return view('notereport_list');
    }

    public function newre_del()
    {
        $id = input('get.id');
        $ret = db('newreport')->where("id={$id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }

    public function newnote_del()
    {
        $id = input('get.id');
        $ret = db('notereport')->where("id={$id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }

    public function change_accept(){
        $id = $_GET['id'];
        $is_accept = $_GET['is_accept'];
        $ret = db('newreport')->where("id = {$id}")->update(array('is_accept'=>$is_accept));
        if($ret){
            if($is_accept==1){
                $result = $this->publish_notice($id);
                if($result){
                    return [
                        'msg' => '更新成功',
                        'status' => 1
                    ];
                }else{
                    return [
                        'msg' => '更新失败',
                        'status' => 0
                    ];
                }
            }else{
                return [
                    'msg' => '更新成功',
                    'status' => 1
                ];
            }
        }else{
            return [
                'msg' => '更新失败',
                'status' => 0
            ];
        }
    }

    public function publish_notice($id){
        $ret = db('newreport')->where("id = {$id}")->find();
        $new_info = db('new')->where("new_id={$ret['new_id']}")->find();
        $str = '《'.$new_info['title'].'》'.'存在'.$ret['type'].'的违规行为，管理员已经将其删除';
        $data = [
            'title'   => '举报公告',
            'content' => $str,
            'addtime' => time()
        ];
        $ret = db('notice')->insert($data);
        if($ret){
            return true;
        }else{
            return false;
        }
    }

    public function change_accept2(){
        $id = $_GET['id'];
        $is_accept = $_GET['is_accept'];
        $ret = db('notereport')->where("id = {$id}")->update(array('is_accept'=>$is_accept));
        if($ret){
            if($is_accept==1){
                $result = $this->publish_notice2($id);
                if($result){
                    return [
                        'msg' => '更新成功',
                        'status' => 1
                    ];
                }else{
                    return [
                        'msg' => '更新失败',
                        'status' => 0
                    ];
                }
            }else{
                return [
                    'msg' => '更新成功',
                    'status' => 1
                ];
            }
        }else{
            return [
                'msg' => '更新失败',
                'status' => 0
            ];
        }
    }

    public function publish_notice2($id){
        $ret = db('notereport')->where("id = {$id}")->find();
        $note_info = db('note')->where("note_id={$ret['note_id']}")->find();
        $str = '《'.$note_info['title'].'》'.'存在'.$ret['type'].'的违规行为，管理员已经将其删除';
        $data = [
            'title'   => '举报公告',
            'content' => $str,
            'addtime' => time()
        ];
        $ret = db('notice')->insert($data);
        if($ret){
            return true;
        }else{
            return false;
        }
    }

    public function report_type_list()
    {   
        $ret = db('new_report_type')->order("addtime desc")->paginate(4);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',12);
        return view('report_type_list');
    }

    public function retype_del()
    {
        $retype_id = input('get.retype_id');
        $ret = db('new_report_type')->where("retype_id={$retype_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }
    
    public function add_retype(){
        $this->assign('pick',12);
        return view('add_retype');
    }

    public function add_retype_action(){
        $retype_name     = input('post.retype_name');

        $data = [
            'retype_name'   => $retype_name,
            'addtime'       => time()
        ]; 
        
        $ret = db('new_report_type')->insert($data);
        if($ret ==false)
        {
            $this->error("添加类型失败！");
        }
        $this->success("添加类型成功！",'/admin/report/report_type_list');
    }
}
